package team.pwy.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.pwy.consts.RbmConsts;
import team.pwy.mapper.CouponMapper;
import team.pwy.mapper.OrderMapper;
import team.pwy.mapper.SeatMapper;
import team.pwy.mapper.SessionMapper;
import team.pwy.pojo.dto.OrderDTO;
import team.pwy.pojo.entity.Order;
import team.pwy.pojo.entity.Seat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SeatMapper seatMapper;

    @Autowired
    private SessionMapper sessionMapper;

    @Autowired
    private CouponMapper couponMapper;


    @Override
    public PageInfo<Order> findByUserId(Integer userId, Integer pageNum, Integer pageSize) {

        PageHelper.startPage(pageNum, pageSize);

        List<Order> orderList = orderMapper.findByUserId(userId);


        return new PageInfo<>(orderList);
    }

    @Override
    public Integer addOrder(OrderDTO orderDTO) {

        Integer couponId = orderDTO.getCouponId();

        Map<String, Object> orderMap = new HashMap<>();
        Order order = new Order(orderDTO.getId(), orderDTO.getNumber(), orderDTO.getUserId(), orderDTO.getRealName(),
                orderDTO.getPhone(), orderDTO.getMovieId(), orderDTO.getMovieTitle(), orderDTO.getPosterImg(),
                orderDTO.getMoviePrice(), orderDTO.getQuota(), orderDTO.getTotal(), orderDTO.getStatus(), orderDTO.getSessionId(), orderDTO.getCouponId(), orderDTO.getCouponMoney(), orderDTO.getCreateTime());
        //        未超时
        if (orderDTO.getStatus() != 2) {
            orderMapper.addOrder(order);
            orderMap.put("orderId", order.getId());

            //        支付超时
        }

        List<Integer> seatList = orderDTO.getSeatId();
        Integer[] ids = new Integer[seatList.size()];
        for (int i = 0; i < seatList.size(); i++) {
            ids[i] = seatList.get(i);
        }
        orderMap.put("ids", ids);

        if (orderDTO.getStatus() != 2) {
            orderMap.put("payStatus", 1);
            seatMapper.updateByIds(orderMap);
            sessionMapper.updateById(orderDTO.getSessionId(), -orderDTO.getQuota());
            if (couponId != null) {
                couponMapper.updateByCouponId(1, couponId);
            }
        }
        return (Integer) orderMap.get("orderId");
    }

    @Override
    public Order findById(Integer id) {
        return orderMapper.findById(id);
    }

    @Override
    public Integer updateById(Integer id, Integer payStatus) {
        return orderMapper.updateById(id, payStatus);
    }

    @Override
    public Integer deleteByIdAndUerId(Integer id, Integer userId) {
        return orderMapper.deleteByIdAndUserId(id, userId);
    }

    @Override
    public Order findByNumber(String number) {
        return orderMapper.findByNumber(number);
    }

    @Override
    public List<Integer> findByOrderId(Integer orderId) {
        return seatMapper.findByOrderId(orderId);
    }

    @Override
    public Integer updateByNum(String number) {
        return orderMapper.updateByNum(number);
    }
}
